package com.holly.unit.deform.modular.controller;

import cn.hutool.core.util.StrUtil;
import com.holly.unit.core.pojo.response.ResponseData;
import com.holly.unit.core.pojo.response.SuccessResponseData;
import com.holly.unit.db.api.pojo.druid.DruidProperties;
import com.holly.unit.db.api.pojo.page.PageResult;
import com.holly.unit.deform.api.DeformComponentApi;
import com.holly.unit.deform.api.pojo.component.DeformDeptTreeDTO;
import com.holly.unit.deform.api.pojo.component.DeformDictDTO;
import com.holly.unit.deform.api.pojo.component.DeformUserDTO;
import com.holly.unit.deform.modular.pojo.DesignFromTableDataRequest;
import com.holly.unit.deform.util.TokenUtil;
import com.holly.unit.dict.modular.pojo.request.DictRequest;
import com.holly.unit.jwt.api.exception.JwtException;
import com.holly.unit.jwt.api.exception.enums.JwtExceptionEnum;
import com.holly.unit.scanner.api.annotation.ApiResource;
import com.holly.unit.scanner.api.annotation.GetResource;
import com.holly.unit.scanner.api.annotation.PostResource;
import com.holly.unit.system.api.pojo.user.request.SysUserRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类描述: 表单组件扩展控制器
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/4/15 15:45
 */
@Slf4j
@RestController
@ApiResource(name = "表单组件扩展")
@Api(tags = "表单组件扩展")
public class DeformComponentController {

    @Resource
    private DeformComponentApi api;


    @Resource
    private DruidProperties druidProperties;


    /**
     * 根据TOKEN获取用户的部分信息（返回的数据是可供表单设计器使用的数据）
     *
     * @return
     */
    @ApiOperation(value = "根据TOKEN获取用户的部分信息", notes = "根据TOKEN获取用户的部分信息")
    @GetResource(name = "根据TOKEN获取用户的部分信息", path = "/sys/user/getUserSectionInfoByToken")
    public ResponseData getUserSectionInfoByToken(HttpServletRequest request) {

        String token = TokenUtil.getTokenByRequest(request);
        if (StrUtil.isBlank(token)) {
            throw new JwtException(JwtExceptionEnum.JWT_PARSE_ERROR, token);
        }
        Map<String, Object> map = api.fetchUserSectionInfoByToken(token);
        return new SuccessResponseData(map);
    }

    /**
     * （表单设计器获取所有关联表）
     *
     * @return
     */
    @ApiOperation(value = "表单设计器获取所有关联表", notes = "表单设计器获取所有关联表")
    @GetResource(name = "表单设计器获取所有关联表", path = "/sys/db/tableInfo")
    public ResponseData getTableInfoByToken(HttpServletRequest request) {

        String token = TokenUtil.getTokenByRequest(request);
        if (StrUtil.isBlank(token)) {
            throw new JwtException(JwtExceptionEnum.JWT_PARSE_ERROR, token);
        }
        Map<String, Object> map = api.fetchDbTableListInfo(token, druidProperties);
        return new SuccessResponseData(map);
    }

    /**
     * （表单设计器获取所有关联表）
     *
     * @return
     */
    @ApiOperation(value = "表单设计器获取所有关联表字段信息", notes = "表单设计器获取所有关联表字段信息")
    @GetResource(name = "表单设计器获取所有关联表字段信息", path = "/sys/db/tableColsInfo")
    public ResponseData getTableFieldInfoByToken(HttpServletRequest request) {

        String token = TokenUtil.getTokenByRequest(request);
        if (StrUtil.isBlank(token)) {
            throw new JwtException(JwtExceptionEnum.JWT_PARSE_ERROR, token);
        }
        String tableName = request.getParameter("tableName");
        Map<String, Object> map = api.fetchDbTableColListInfo(token, druidProperties, tableName);
        return new SuccessResponseData(map);
    }


    @ApiOperation(value = "表单设计器获取表中数据分页查询", notes = "表单设计器获取表中数据分页查询")
    @PostResource(name = "表单设计器获取所有关联表字段信息", path = "/sys/db/getTableDataByColumn")
    public ResponseData getTableDataByColumn(@RequestBody DesignFromTableDataRequest designFromTableDataRequest) {
        PageResult<Map> pageResult = api.getTableDataByColumn(
                designFromTableDataRequest.getPageNo(),
                designFromTableDataRequest.getPageSize(),
                designFromTableDataRequest.getTableName(),
                designFromTableDataRequest.getColumnList());
        return new SuccessResponseData(pageResult);
    }


    /**
     * 根据TOKEN获取用户的部分信息（返回的数据是可供表单设计器使用的数据）
     *
     * @return
     */
    @ApiOperation(value = "根据TOKEN获取用户的部分信息测试", notes = "根据TOKEN获取用户的部分信息测试")
    @GetResource(name = "根据TOKEN获取用户的部分信息测试", path = "/sys/user/getUserSectionInfoByTokenTest")
    public ResponseData getUserSectionInfoByTokenTest(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("sysUserId", "1339550467939639299");
        map.put("sysUserCode", "admin"); // 当前登录用户登录账号
        map.put("sysUserName", "admin管理员"); // 当前登录用户真实名称
        map.put("sysOrgCode", ""); // 当前登录用户部门编号
        return new SuccessResponseData(map);
    }


    /**
     * 分页查询系统用户
     */
    @ApiOperation(value = "表单设计器用户组件", notes = "表单设计器用户组件")
    @GetResource(name = "表单设计器用户组件", path = "/sys/user/list")
    public ResponseData getPage(SysUserRequest sysUserRequest) {
        PageResult<DeformUserDTO> pageResult = api.fetchPageSysUsers(sysUserRequest);
        return new SuccessResponseData(pageResult);
    }

    /**
     * 表单设计器获取字典列表(分页)
     */
    @ApiOperation(value = "表单设计器获取字典列表", notes = "表单设计器获取字典列表")
    @GetResource(name = "表单设计器获取字典列表", path = "/sys/dict/list")
    public ResponseData page(DictRequest dictRequest) {
        PageResult<DeformDictDTO> pageResult = this.api.fetchPageDict(dictRequest);
        return new SuccessResponseData(pageResult);
    }

    /**
     * 查询数据 查出所有部门,并以树结构数据格式响应给前端
     *
     * @return
     */
    @ApiOperation(value = "表单设计器部门组件", notes = "表单设计器部门组件")
    @GetResource(name = "表单设计器部门组件", path = "/sys/sysDepart/queryTreeList")
    public ResponseData queryTreeList() {
        List<DeformDeptTreeDTO> dtos = api.fetchTreeList();
        return new SuccessResponseData(dtos);
    }

}
